package com.hdoit.kylin.logical.money.dao.impl;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.money.dao.IAssetDao;
import com.hdoit.kylin.logical.money.model.Asset;
import com.hdoit.kylin.logical.viewmodel.money.AssetSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
*@author liyaochong
*@email
*@createtime 2017年07月27日 14:56:25
*@comment
*/
@Component
public class AssetDaoImpl extends SuperDao<Asset> implements IAssetDao{

	@Override
	public int queryCountByGroupId(AssetSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("count(DISTINCT asset.assetId) as count")
										 .from(Asset.class,"asset")
										 .leftJoin(Group.class,"group_info")
										 .on("asset.groupId","group_info.groupId")
										 .andWhere("asset.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("asset");
		}
		else{
			finderExpression.select("count(DISTINCT asset.assetId) as count")
										 .from(Asset.class,"asset")
										 .leftJoin(Group.class,"group_info")
										 .on("asset.groupId","group_info.groupId")
										 .andWhere("group_info.groupId IS NOT NULL")
										 .setMainTableAlias("asset");		
		}
		return queryCount(finderExpression,search);	
	}

	@Override
	public List<Asset> queryListByGroupId(AssetSearch search,PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("asset.*,group_info.groupName as groupName")
										 .from(Asset.class,"asset")
										 .leftJoin(Group.class,"group_info")
										 .on("asset.groupId","group_info.groupId")
										 .andWhere("asset.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("asset");		
		}
		else{
			finderExpression.select("asset.*,group_info.groupName as groupName")
										 .from(Asset.class,"asset")
										 .leftJoin(Group.class,"group_info")
										 .on("asset.groupId","group_info.groupId")
										 .andWhere("group_info.groupId IS NOT NULL")
										 .setMainTableAlias("asset");		
		}
		return queryList(finderExpression,search,pagerModel);
	}
}